'use strict';

const { createBlogFailInfo } = require('../utils/ErrorModel');
const { ErrorModel, SuccessModel } = require('../utils/resultModel');

const PAGE_SIZE = 5;
// egg-tip插件
module.exports = app => {
  class BlogController extends app.Controller {

    // 创建博客
    async createBlog() {
      const { ctx } = this;
      // 3. 获取提交的数据 内容和地址
      let { content, image } = ctx.request.body;
      // XSS 防范
      content = ctx.helper.escape(content);
      // 还需要userId
      const { id: userId } = ctx.session.userInfo;

      const REG_AT_WHO = /@(.+?)\s-\s(\w+?)\b/g;
      // 获取 at 用户名列表
      const atUserNameList = [];

      content.replace(REG_AT_WHO, (matchStr, nickName, userName) => {
        atUserNameList.push(userName);
        return matchStr;
      });

      // 获取用户列表
      const atUserList = await Promise.all(atUserNameList.map(async name => {
        const result = await this.service.user.getUserInfo(name);
        return result;
      }));

      // 获取 用户id 列表 [1,2,3,6,5,];
      const atUserIdList = atUserList.map(user => user.id);

      try {
        // 4. 将数据交给service处理 create
        const result = await this.service.blog.create({ content, userId, image });

        // 向 atRelation 表中写数据
        atUserIdList.map(async userId => {
          await this.service.atRelation.createAtRelation(result.id, userId);
        });


        ctx.body = new SuccessModel(result);
      } catch (err) {
        console.log(err);
        ctx.body = new ErrorModel(createBlogFailInfo);
      }
    }

    // 加载更多
    async loadProfileMore() {
      // 获取参数
      let { userName, pageIndex } = this.ctx.params;
      // 传递过来的是字符串，做类型转换
      pageIndex = parseInt(pageIndex);

      // 定义每页的显示数量
      const pageSize = PAGE_SIZE;

      // 获取博客信息 调用service 中的方法
      const result = await this.service.blog.getProfileBlogList({
        userName,
        pageIndex,
        pageSize,
      });
      // 响应数据
      this.ctx.body = new SuccessModel({
        blogList: result.blogList,
        count: result.count,
        pageIndex: pageIndex + 1,
        pageSize,
      });
    }

    // 加载广场页更多数据
    async loadSquareMore() {
      // 获取参数
      let { pageIndex } = this.ctx.params;
      // 传递过来的是字符串，做类型转换
      pageIndex = parseInt(pageIndex);
      // 定义每页的显示数量
      const pageSize = 10;

      // 获取博客信息 调用service 中的方法
      const result = await this.service.blog.getSquareBlogList({
        pageIndex,
        pageSize,
      });
      // 响应数据
      this.ctx.body = new SuccessModel({
        blogList: result.blogList,
        count: result.count,
        pageIndex: pageIndex + 1,
        pageSize,
      });
    }

    // 首页加载更多
    async loadHomeBlogMore() {
      // 在session中获取用户id
      const { id } = this.ctx.session.userInfo;
      // 获取pageIndex
      let { pageIndex } = this.ctx.params;
      // '/api/blog/loadHomeBlogMore/:pageIndex'
      pageIndex = parseInt(pageIndex);
      // 获取关注人博客
      const result = await this.service.blog.getFollowerBlogList({
        userId: id,
        pageIndex,
      });
      this.ctx.body = new SuccessModel({
        blogList: result.blogList,
        count: result.count,
        pageIndex: pageIndex + 1,
        pageSize: 10,
      });
    }

    // atme 加载更多
    async loadAtMeBlogMore() {
      // userId
      const { id: userId } = this.ctx.session.userInfo;
      // pageIndex
      const { pageIndex } = this.ctx.params;
      // getAtMeBlogList({userId,pageIndex,pageSize})
      const result = await this.service.atRelation.getAtMeBlogList({ userId, pageIndex });

      // {errno:0,data:{pageSize: 5,}}
      this.ctx.body = new SuccessModel({
        blogList: result.blogList,
        count: result.count,
        pageIndex: pageIndex + 1,
        pageSize: 5,
      });
    }
  }
  return BlogController;
};

